package com.bright.ghj.overt.web.controller;

import com.bright.ghj.common.pojo.bo.RestResult;
import com.bright.ghj.overt.util.SecurityUtil;
import com.bright.ghj.common.util.StringUtil;
import com.bright.ghj.overt.pojo.command.OpenReportOperateIdsCommand;
import com.bright.ghj.overt.pojo.command.YearsMonthsZtIdCommand;
import com.bright.ghj.overt.pojo.query.TableDataIdQuery;
import com.bright.ghj.overt.service.OperateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @Author hxj
 * @Date 2024/1/4 11:09
 * @Description
 */
@RequestMapping("/operate")
@RestController
@RequiredArgsConstructor
@Api(tags = "流程操作接口")
public class OperateController {

    @Qualifier("operateNoFlow")
    private final OperateService operateService;

//    private final NavigateService navigateService;

    /**
     * 提交报审 支持批量
     * @return
     */
    @ApiOperation(value = "提交报审")
    @PostMapping("/readyAudit")
    public RestResult readyAudit(@RequestBody @Valid OpenReportOperateIdsCommand command) {
        command.setOperator(getUserName());
        operateService.readyAudit(command);
        return RestResult.success();
    }

    /**
     * 审核数据
     * @param command
     * @return
     */
    @PreAuthorize("hasAnyAuthority('cwgkAudit','dwgkAudit','swgkAudit') && hasAnyAuthority('isPermitAdmin')")
    @ApiOperation(value = "审核数据")
    @PostMapping("/audit")
    public RestResult audit(@RequestBody @Valid OpenReportOperateIdsCommand command) {
        if (StringUtil.isEmpty(command.getOpinion())) {
            return RestResult.fail("意见不能为空");
        }
        command.setOperator(getUserName());
        try {
            operateService.audit(command);
        } catch (Exception e) {
            return RestResult.fail("操作失败：" + e.getMessage());
        }
        return RestResult.success();
    }

    /**
     * 取消审核
     * @param command
     * @return
     */
    @PreAuthorize("hasAnyAuthority('cwgkAudit','dwgkAudit','swgkAudit') && hasAnyAuthority('isPermitAdmin')")
    @ApiOperation(value = "取消审核数据")
    @PostMapping("/cancelAudit")
    public RestResult cancelAudit(@RequestBody @Valid OpenReportOperateIdsCommand command) {
        if (StringUtil.isEmpty(command.getOpinion())) {
            return RestResult.fail("意见不能为空");
        }
        command.setOperator(getUserName());
        try {
            operateService.cancelAudit(command);
        } catch (Exception e) {
            return RestResult.fail("操作失败：" + e.getMessage());
        }
        return RestResult.success();
    }

    /**
     *
     * @param command
     * @return
     */
    @PreAuthorize("hasAnyAuthority('cwgkPublish','dwgkPublish','swgkPublish') && hasAnyAuthority('isPermitAdmin')")
    @ApiOperation(value = "发布数据")
    @PostMapping("/publish")
    public RestResult publish(@RequestBody @Valid OpenReportOperateIdsCommand command) {
        command.setOperator(getUserName());
        try {
            operateService.publish(command);
        } catch (Exception e) {
            return RestResult.fail("操作失败：" + e.getMessage());
        }
        return RestResult.success();
    }

    /**
     *
     * @param command
     * @return
     */
    @PreAuthorize("hasAnyAuthority('cwgkPublish','dwgkPublish','swgkPublish') && hasAnyAuthority('isPermitAdmin')")
    @ApiOperation(value = "取消发布数据")
    @PostMapping("/cancelPublish")
    public RestResult cancelPublish(@RequestBody @Valid OpenReportOperateIdsCommand command) {
        if (StringUtil.isEmpty(command.getOpinion())) {
            return RestResult.fail("意见不能为空");
        }
        command.setOperator(getUserName());
        try {
            operateService.cancelPublish(command);
        } catch (Exception e) {
            return RestResult.fail("操作失败：" + e.getMessage());
        }
        return RestResult.success();
    }

    /**
     * 获取操作记录
     * @param tableDataIdQuery
     * @return
     */
    @PreAuthorize("hasAnyAuthority('isPermitAdmin')")
    @ApiOperation(value = "获取操作记录")
    @PostMapping("/log/list")
    public RestResult listLogs(@RequestBody @Valid TableDataIdQuery tableDataIdQuery) {
//        List<HistoryTaskDTO> historyTaskDTOS = operateService.listLogs(tableDataIdQuery);
//        List<FlowLogVO> result = new ArrayList<>();
//        for (HistoryTaskDTO historyTaskDTO : historyTaskDTOS) {
//            FlowLogVO historyTaskVO = new FlowLogVO();
//            historyTaskVO.setStartTime(historyTaskDTO.getStartTime());
//            historyTaskVO.setEndTime(historyTaskDTO.getEndTime());
//            historyTaskVO.setVariables(historyTaskDTO.getVariables());
//            historyTaskVO.setName(historyTaskDTO.getName());
//            result.add(historyTaskVO);
//        }

        return RestResult.success(operateService.listLogs(tableDataIdQuery));
    }


    /**
     * 审核数据
     * @param command
     * @return
     */
    @PreAuthorize("hasAnyAuthority('cwgkAudit','dwgkAudit','swgkAudit') && hasAnyAuthority('isPermitAdmin')")
    @ApiOperation(value = "按单位批量审核数据")
    @PostMapping("/auditByZts")
    public RestResult auditByZts(@RequestBody @Valid YearsMonthsZtIdCommand command) {
        if (StringUtil.isEmpty(command.getOpinion())) {
            return RestResult.fail("意见不能为空");
        }
        command.setOperator(getUserName());
        operateService.audit(command);
        return RestResult.success();
    }


    /**
     * 取消审核数据
     * @param command
     * @return
     */
    @PreAuthorize("hasAnyAuthority('cwgkAudit','dwgkAudit','swgkAudit') && hasAnyAuthority('isPermitAdmin')")
    @ApiOperation(value = "按单位批量取消审核数据")
    @PostMapping("/cancelAuditByZts")
    public RestResult cancelAuditByZts(@RequestBody @Valid YearsMonthsZtIdCommand command) {
        if (StringUtil.isEmpty(command.getOpinion())) {
            return RestResult.fail("意见不能为空");
        }
        command.setOperator(getUserName());
        operateService.cancelAudit(command);
        return RestResult.success();
    }

    /**
     * 发布数据
     * @param command
     * @return
     */
    @PreAuthorize("hasAnyAuthority('cwgkPublish','dwgkPublish','swgkPublish') && hasAnyAuthority('isPermitAdmin')")
    @ApiOperation(value = "按单位批量发布数据")
    @PostMapping("/publishByZts")
    public RestResult publishByZts(@RequestBody @Valid YearsMonthsZtIdCommand command) {
        command.setOperator(getUserName());
        operateService.publish(command);
        return RestResult.success();
    }

    /**
     * 取消发布数据
     * @param command
     * @return
     */
    @PreAuthorize("hasAnyAuthority('cwgkPublish','dwgkPublish','swgkPublish') && hasAnyAuthority('isPermitAdmin')")
    @ApiOperation(value = "按单位批量取消发布数据")
    @PostMapping("/cancelPublishByZts")
    public RestResult cancelPublishByZts(@RequestBody @Valid YearsMonthsZtIdCommand command) {
        if (StringUtil.isEmpty(command.getOpinion())) {
            return RestResult.fail("意见不能为空");
        }
        command.setOperator(getUserName());
        operateService.cancelPublish(command);
        return RestResult.success();
    }

    // 获取登录用户名
    private String getUserName() {
        return SecurityUtil.getLoginUser().getUsername();
//        return "超级用户";
//        return navigateService.getLoginUser().getUsername();
    }

}
